//
//  EFPayTypeChooseViewController.m
//  EnjogFood
//
//  Created by guocheng on 2017/5/28.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFPayTypeChooseViewController.h"
#import "EFPayTypeChooseViewCell.h"
#import "EFPayTypeChooseViewItem.h"

@interface EFPayTypeChooseViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(strong,nonatomic)NSMutableArray* dataArray;
@property(strong,nonatomic)UITableView* tableView;

@property(strong,nonatomic)UIView* topView;
@property(strong,nonatomic)UILabel* titleLabel;

@end

@implementation EFPayTypeChooseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self initData];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.title = @"支付方式";
        self.dataArray = [NSMutableArray array];
    }
    return self;
}

-(void)initView{
    [self.topView addSubview:self.titleLabel];
    [self.view addSubview:self.topView];
    [self.view addSubview:self.tableView];
}

-(void)initData{
    EFPayTypeChooseViewItem* wx =[[EFPayTypeChooseViewItem alloc]init];
    wx.title = @"微信";
    wx.isSelect = true;
    [self.dataArray addObject:wx];
    
//   EFPayTypeChooseViewItem* zfb =[[EFPayTypeChooseViewItem alloc]init];
//    zfb.title = @"支付宝";
//    [self.dataArray addObject:zfb];
    
    [self.tableView reloadData];
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ICTableViewItem *item = self.dataArray[indexPath.row];
    return item.viewHeight;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dataArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ICTableViewItem* item = self.dataArray[indexPath.row];
    NSString *identifier = NSStringFromClass(item.viewClass);
    ICTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil){
        cell = [[item.viewClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if ([item isKindOfClass:[ICTableViewItem class]]) {
        [cell setObject:item];
    }
    if ([item isKindOfClass:[EFPayTypeChooseViewItem class]]) {
        ((EFPayTypeChooseViewCell *)cell).selectblock = ^(NSString* title){
            for (NSObject *object in self.dataArray) {
                if([object isKindOfClass:[EFPayTypeChooseViewItem class]]){
                    EFPayTypeChooseViewItem* chooseItem = (EFPayTypeChooseViewItem*)object;
                    if([chooseItem.title isEqualToString:title]){
                        chooseItem.isSelect = true;
                    }else{
                         chooseItem.isSelect = false;
                    }
                }
            }
            [self.tableView reloadData];
        };
    }
    
    return cell;
}


- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kStatusBarHeight- 50 )];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.top =   self.topView.bottom;
        
    }
    return _tableView;
}

-(UIView *)topView{
    if(_topView == NULL){
        _topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
        _topView.backgroundColor = RGBCOLOR(0xF5F9F9);
    }
    return _topView;
}

-(UILabel *)titleLabel{
    if(_titleLabel == NULL){
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.textColor = RGBCOLOR(0x7EAD6A);
        _titleLabel.font = [UIFont systemFontOfSize:15];
        [_titleLabel setText:@"选择支付方式"];
        [_titleLabel sizeToFit];
        _titleLabel.top = (self.topView.height - _titleLabel.height)/2;
        _titleLabel.left = 20;
    }
    return _titleLabel;
}


@end
